package com.dstation.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dstation.domain.AdministratorInfo;
import com.dstation.domain.ClassificationInfo;
import com.dstation.domain.UserInfo;
import com.dstation.service.interf.IClassificationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import javax.websocket.server.PathParam;
import java.util.Objects;

/**
 * @author:  Jeadon
 * @create:  2022/4/8 8:16
 * @tip:  分类controller层
 *
 * @param:
 *
 **/
@Slf4j
@Controller
@RequestMapping(value = "/classification")
public class ClassificationController {
    final  int pageSize=10;

    @Autowired
    private IClassificationService classificationService;


    @RequestMapping(value = "viewClassification")
    public String viewClassification(@PathParam("page")Integer page, @PathParam("search")String search, ModelMap modelMap){

        if(Objects.isNull(page)){
            return "";
        }

        IPage<ClassificationInfo> pageData=classificationService.findClassification(search,new Page(page,pageSize));

        modelMap.put("pageData",pageData);

        return "admin/classification";

    }


    @ResponseBody
    @RequestMapping(value = "save")
    public JSONObject saveClassification( @PathParam("classification")ClassificationInfo classificationInfo){
        JSONObject jsonObject=new JSONObject();

//        AdministratorInfo administratorInfo=(AdministratorInfo) httpSession.getAttribute("userInfo");

        boolean isRepeat=isClassificationNameRepeat(classificationInfo.getClassificationName());
        boolean isHave=true;
        if(classificationInfo.getPreClassificationId()!=0){
            isHave=isClassificationPreId(classificationInfo.getPreClassificationId());
        }

        boolean isSave=false;

        if(!isRepeat&&isHave){
            isSave=classificationService.addClassification(classificationInfo);
        }

        jsonObject.put("isSave",isSave);

        return jsonObject;
    }



    @ResponseBody
    @RequestMapping(value = "update")
    public JSONObject modifyClassification( @PathParam("oldName") String oldName,@PathParam("newName")String newName){
        JSONObject jsonObject=new JSONObject();


        boolean isRepeat=isClassificationNameRepeat(newName);
        boolean isModify=false;

        if(!isRepeat){
            Long id=classificationService.findIdByName(oldName);

            isModify=classificationService.alterClassification(new ClassificationInfo(id,newName));
        }

        jsonObject.put("isModify",isModify);

        return jsonObject;
    }

    @ResponseBody
    @RequestMapping(value = "delete")
    public JSONObject removeClassification( @PathParam("classificationId")long classificationId){
        JSONObject jsonObject=new JSONObject();

        boolean isRemove=false;


        isRemove=classificationService.dropClassification(classificationId);


        jsonObject.put("isDelete",isRemove);

        return jsonObject;
    }
    private boolean isClassificationPreId(Long preClassificationId) {
        return classificationService.findIsHavePreId(preClassificationId);
    }
    private boolean isClassificationNameRepeat(String classificationName) {
        return classificationService.findIsClassificationNameRepeat(classificationName);
    }

}
